package com.codeoss.framework.db.util;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.module.ir.model.IrColumnChangeLog;
import com.codeoss.module.ir.model.IrModel;
import com.codeoss.module.ir.service.IIrColumnChangeLogService;
import com.codeoss.module.ir.service.IIrModelService;

@Component
public class ColumnChangeLoggingUtil {
	@Autowired
	private IIrColumnChangeLogService irColumnChangeLogService;
	@Autowired
	private IIrModelService irModelService;
	@Autowired
	private IConverterStore converterStore;
	@Transactional
	public void logDataInsert(YunContext yunContext,Table table,Map<String,Object> dataMap) {
		List<Column> loggingColumnList=table.getLoggingColumnList();
		if(loggingColumnList.size()>0) {
			IrModel irModel=irModelService.getIrModelByTableName(table.getTableName());
			for(int i=0;i<loggingColumnList.size();i++) {
				Column column=loggingColumnList.get(i);
				Object newValue=dataMap.get(column.getColumnName());
				if(newValue!=null) {
					IrColumnChangeLog irColumnChangeLog=new IrColumnChangeLog();
					irColumnChangeLog.setIrModelId(irModel.getId());
					irColumnChangeLog.setColumnName(column.getColumnName());
					irColumnChangeLog.setRecordId((Long)dataMap.get(table.getPrimaryKeyColumn().getColumnName()));
					irColumnChangeLog.setOldValue(null);
					
					IConverter converter=converterStore.getConverter(column.getDataType());
					String sNewValue=converter.convertObjectToString(newValue);
					irColumnChangeLog.setNewValue(sNewValue);
					irColumnChangeLogService.insert(irColumnChangeLog);
				}
			}
		}
	}
	@Transactional
	public void logDataUpdate(YunContext yunContext,Table table,Map<String,Object> oldDataMap,Map<String,Object> newDataMap) {
		List<Column> loggingColumnList=table.getLoggingColumnList();
		if(loggingColumnList.size()>0) {
			IrModel irModel=irModelService.getIrModelByTableName(table.getTableName());
			for(int i=0;i<loggingColumnList.size();i++) {
				Column column=loggingColumnList.get(i);
				Object oldValue=oldDataMap.get(column.getColumnName());
				Object newValue=newDataMap.get(column.getColumnName());
				boolean isEquals=true;
				if(oldValue!=null && !oldValue.equals(newValue)) {
					isEquals=false;
				}else if(newValue!=null && !newValue.equals(oldValue)) {
					isEquals=false;
				}
				if(!isEquals) {
					IrColumnChangeLog irColumnChangeLog=new IrColumnChangeLog();
					irColumnChangeLog.setIrModelId(irModel.getId());
					irColumnChangeLog.setColumnName(column.getColumnName());
					irColumnChangeLog.setRecordId((Long)oldDataMap.get(table.getPrimaryKeyColumn().getColumnName()));
					IConverter converter=converterStore.getConverter(column.getDataType());
					if(oldValue!=null) {
						String sOldValue=converter.convertObjectToString(oldValue);
						irColumnChangeLog.setOldValue(sOldValue);
					}
					if(newValue!=null) {
						String sNewValue=converter.convertObjectToString(newValue);
						irColumnChangeLog.setNewValue(sNewValue);
					}
					irColumnChangeLogService.insert(irColumnChangeLog);
				}
			}
		}
	}
}
